package com.simplesoft.accountbook;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseOpenHelper extends SQLiteOpenHelper {
	private static final int DATABASE_VERSION = 2;
	private static final String ACCOUNT_BOOK_TABLE_NAME = "money_record";
	private static final String ACCOUNT_BOOK_TABLE_CREATE = "CREATE TABLE " + 
			ACCOUNT_BOOK_TABLE_NAME + 
			" (rid INTEGER PRIMARY KEY AUTOINCREMENT, year INTEGER, month INTEGER, day INTEGER, amount INTEGER);";
	
	public MyDatabaseOpenHelper (Context context) {
		super(context, "account_book", null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(ACCOUNT_BOOK_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion < 3) {
			db.execSQL("BEGIN TRANSACTION;" +
					"CREATE TEMPORARY TABLE tmp (rid, year, month, day, amount);" +
					"INSERT INTO tmp SELECT rid, year, month, day, amount from money_record;" +
					"DROP TABLE money_record;" +
					"CREATE money_record (rid INTEGER PRIMARY KEY, year, month, day, amount);" +
					"INSERT INTO money_record select rid, year, month, day, amount from tmp;" +
					"DROP TABLE tmp;" +
					"COMMIT;");
			
		}
	}

}
